Creating custom collate function